Crate stable_hash

source ·
Expand description

This crate provides a stable, structured hash with backward compatibility features. What does that mean?

  • Stable: The value of the hash will not change across minor versions of this library, even when the compiler, process, architecture, or std lib does change.
  • Structured: Hashes structs, rather than streams
  • Backward compatibility: It is possible to make limited changes to a struct’s schema without changing the value of the hash. One change is that you can add new fields. This is accomplished by skipping default values when hashing. For example, the values None, 0, false, and vec![] do not contribute to the hash. Therefore structs Old { a: 1 } and New { a: 1, b: None } hash to the same value. Another feature enabling backward compatibility is that the size of an integer can be increased without changing the value. For example, Old { a: 1u16 } and New { a: 1u32 } hash to the same value. Note that even though two structs with different schemas are allowed to collide, two structs with the same schema never collide (where collide is defined as contribution to the hash is injective in respect to the encoding. It is still possible to find collisions in the final output, especially for the non-cryptographic version)

Modules

Macros

Implements StableHash. This macro supports two forms: Struct { field1, field2, … } and Tuple(transparent). Each field supports an optional modifier. For example: Tuple(transparent: AsBytes)

Traits

Tracks the path from the root of a struct to a member value. For example, within the value vec![ { num: 0, string: “Alice” }, { num: 1, string: “Bob” } ], the value Alice exists at the path:
Like Hash, but consistent across:
Like Hasher, but consistent across:

Functions